using System;
using System.Runtime.CompilerServices;
using System.Windows;
namespace Microsoft.Phone.Controls.Maps.Core
{
	internal class MapInputManager
	{
		private readonly MapCore parentMap;
		private ManipulationHandler manipulationProcessor;
		private PinchStretchHandler pinchStretchRecognizer;
		internal event EventHandler<MapDragEventArgs> TouchDrag
		{
			[MethodImpl(32)]
			add
			{
				this.TouchDrag = (EventHandler<MapDragEventArgs>)Delegate.Combine(this.TouchDrag, value);
			}
			[MethodImpl(32)]
			remove
			{
				this.TouchDrag = (EventHandler<MapDragEventArgs>)Delegate.Remove(this.TouchDrag, value);
			}
		}
		internal event EventHandler<MapFlickEventArgs> TouchFlick
		{
			[MethodImpl(32)]
			add
			{
				this.TouchFlick = (EventHandler<MapFlickEventArgs>)Delegate.Combine(this.TouchFlick, value);
			}
			[MethodImpl(32)]
			remove
			{
				this.TouchFlick = (EventHandler<MapFlickEventArgs>)Delegate.Remove(this.TouchFlick, value);
			}
		}
		internal event EventHandler<MapZoomEventArgs> TouchZoom
		{
			[MethodImpl(32)]
			add
			{
				this.TouchZoom = (EventHandler<MapZoomEventArgs>)Delegate.Combine(this.TouchZoom, value);
			}
			[MethodImpl(32)]
			remove
			{
				this.TouchZoom = (EventHandler<MapZoomEventArgs>)Delegate.Remove(this.TouchZoom, value);
			}
		}
		internal MapInputManager(MapCore map)
		{
			this.parentMap = map;
			FrameworkElement arg_25_0 = this.parentMap;
			RoutedEventHandler routedEventHandler = delegate(object sender, RoutedEventArgs e)
			{
				this.pinchStretchRecognizer.Enable(true);
			};
			arg_25_0.add_Loaded(routedEventHandler);
			FrameworkElement arg_3E_0 = this.parentMap;
			RoutedEventHandler routedEventHandler2 = delegate(object sender, RoutedEventArgs e)
			{
				this.pinchStretchRecognizer.Enable(false);
				if (this.manipulationProcessor != null)
				{
					this.manipulationProcessor.Unload();
				}
			};
			arg_3E_0.add_Unloaded(routedEventHandler2);
			this.Initialize();
		}
		private void Initialize()
		{
			this.manipulationProcessor = new ManipulationHandler(this.parentMap);
			this.manipulationProcessor.DoubleTap += new EventHandler<GestureEventArgs>(this.OnDoubleTap);
			this.manipulationProcessor.Pan += new EventHandler<GestureEventArgs>(this.OnPan);
			this.manipulationProcessor.Flick += new EventHandler<GestureEventArgs>(this.OnFlick);
			this.pinchStretchRecognizer = new PinchStretchHandler(this.parentMap);
			this.pinchStretchRecognizer.PinchStretchReported += new EventHandler<PinchStretchEventArgs>(this.PinchStretchReported);
		}
		internal void OnDoubleTap(object sender, GestureEventArgs e)
		{
			DoubleTapGestureEventArgs doubleTapGestureEventArgs = e as DoubleTapGestureEventArgs;
			Point arg_0D_0 = doubleTapGestureEventArgs.Origin;
			this.OnTouchZoom(new MapZoomEventArgs(e.Origin, 1.0));
		}
		internal void OnPan(object sender, GestureEventArgs e)
		{
			PanGestureEventArgs panGestureEventArgs = e as PanGestureEventArgs;
			Point origin = panGestureEventArgs.Origin;
			Point translation = panGestureEventArgs.Translation;
			this.OnTouchDrag(new MapDragEventArgs(origin, translation));
		}
		internal void OnFlick(object sender, GestureEventArgs e)
		{
			FlickGestureEventArgs flickGestureEventArgs = e as FlickGestureEventArgs;
			Point origin = flickGestureEventArgs.Origin;
			Point velocity = flickGestureEventArgs.Velocity;
			this.OnTouchFlick(new MapFlickEventArgs(origin, velocity));
		}
		internal void OnScale(object sender, GestureEventArgs e)
		{
			ScaleGestureEventArgs scaleGestureEventArgs = e as ScaleGestureEventArgs;
			Point origin = scaleGestureEventArgs.Origin;
			double num;
			if (scaleGestureEventArgs.Scale.get_X() == 1.0)
			{
				num = scaleGestureEventArgs.Scale.get_Y();
			}
			else
			{
				if (scaleGestureEventArgs.Scale.get_Y() == 1.0)
				{
					num = scaleGestureEventArgs.Scale.get_Y();
				}
				else
				{
					num = (scaleGestureEventArgs.Scale.get_X() + scaleGestureEventArgs.Scale.get_Y()) / 2.0;
				}
			}
			num = Math.Log(num) / Math.Log(2.0);
			this.OnTouchZoom(new MapZoomEventArgs(origin, num));
		}
		internal void PinchStretchReported(object sender, PinchStretchEventArgs e)
		{
			PinchStretchData data = e.Data;
			double zoomDelta = Math.Log(data.Scale) / Math.Log(2.0);
			this.OnTouchZoom(new MapZoomEventArgs(data.CenterPoint, zoomDelta));
		}
		internal void OnTouchDrag(MapDragEventArgs ea)
		{
			EventHandler<MapDragEventArgs> touchDrag = this.TouchDrag;
			if (touchDrag != null)
			{
				touchDrag.Invoke(this.parentMap, ea);
			}
			if (!ea.Handled)
			{
				this.parentMap.Mode.OnMapDrag(ea);
			}
		}
		internal void OnTouchFlick(MapFlickEventArgs ea)
		{
			EventHandler<MapFlickEventArgs> touchFlick = this.TouchFlick;
			if (touchFlick != null)
			{
				touchFlick.Invoke(this.parentMap, ea);
			}
			if (!ea.Handled)
			{
				this.parentMap.Mode.OnMapFlick(ea);
			}
		}
		internal void OnTouchZoom(MapZoomEventArgs ea)
		{
			EventHandler<MapZoomEventArgs> touchZoom = this.TouchZoom;
			if (touchZoom != null)
			{
				touchZoom.Invoke(this.parentMap, ea);
			}
			if (!ea.Handled)
			{
				this.parentMap.Mode.OnMapZoom(ea);
			}
		}
	}
}
